<?php

namespace App\Admin\Controllers;
use App\crm_hetong;
use App\crm_hetongtype;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class Crm_lingyongController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('合同领用管理');
            $content->description('列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('合同领用管理');
            $content->description('编辑');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('合同领用管理');
            $content->description('添加');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(crm_hetong::class, function (Grid $grid) {
            $grid->model()->where('statue', 2);
            $grid->id('序号')->sortable();
            $grid->type('合同类型')->display(function () {
                $list=crm_hetongtype::all()->pluck('title','id');
                return $list[$this->type];
            });
            $grid->hetong('合同号');
            $grid->users_id('领用人');
            $zhuangtai=['2'=>'领用','3'=>'核销'];
            $grid->statue('状态')->display(function () use ($zhuangtai) {
                return $zhuangtai[$this->statue];
            });
            $grid->filter(function($filter){
                // 去掉默认的id过滤器
                $filter->disableIdFilter();
                // 在这里添加字段过滤器
                $list=crm_hetongtype::all()->pluck('title','id');
                $filter->equal('type', '合同类型')->select($list);
                $filter->like('hetong', '合同号');
                $filter->like('users_id', '领用人');
            });
            $grid->disableExport();//禁用导出按钮
            $grid->disableActions();//禁用行操作
            $grid->disableCreation();//禁用新增
            $grid->disableRowSelector();//禁用行选择
        }); 
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(crm_hetong::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->select('statue','状态' )->options(['2'=>'领用','3'=>'核销']);
            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
}
